﻿Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()>
Public Class Car
    Implements ICloneable
    Public Const L3DCarStatusRunning = 1
    Public Const L3DCarStatusStop = 2
    Public Const L3DCarStatusCanceled = 3
    Private NameValue As String
    Private pEOBD As Boolean
    Private pkmHS As Integer
    Private pkmRG As Integer = 25000
    Private pkmCity As Integer
    Private pkmStatus As Integer
    Private pCarStartDate As Date = Now
    Private pCarEndDate As Date = DateAdd(DateInterval.Day, 90, Now)
    Private pCarRealStartDate As Date = Now
    Private pCarRealEndDate As Date = DateAdd(DateInterval.Day, 90, Now)
    Private pCarStatus As Integer
    Private pRGSpeed As Integer = 10000
    Private pHSSpeed As Integer = 20000
    Private pCitySpeed As Integer = 5000
    Public Event StatusChanged(ByVal NewStatus As Integer)
    Public Event DateChanged(ByVal sender As Object, ByVal e As L3DObjectsLibrary.DateChangedEventArgs)
    Public Event QueryChange(ByVal sender As Object, ByVal e As QueryChangeEventArgs)

#Region "PROPERTIES"
    Public Property Name As String
        Get
            Return NameValue
        End Get
        Set(ByVal value As String)
            NameValue = value
        End Set
    End Property
    Public Property CarStatus As Integer
        Get
            If Me.kmCity + Me.kmHS + Me.kmRG <= Me.kmStatus Then
                Return L3DCarStatusStop
            Else
                Return pCarStatus
            End If
        End Get
        Set(ByVal value As Integer)
            pCarStatus = value
            RaiseEvent StatusChanged(value)
        End Set
    End Property
    Public ReadOnly Property HasReplan As Boolean
        Get
            If pCarStartDate = pCarRealStartDate Then
                Return False
            Else
                Return True
            End If
        End Get
    End Property
    Public Property EOBD As Boolean
        Get
            Return pEOBD
        End Get
        Set(ByVal value As Boolean)
            pEOBD = value
        End Set
    End Property
    Public Property kmHS As Integer
        Get
            Return pkmHS
        End Get
        Set(ByVal value As Integer)
            pkmHS = value
        End Set
    End Property
    Public Property kmRG As Integer
        Get
            Return pkmRG
        End Get
        Set(ByVal value As Integer)
            pkmRG = value
        End Set
    End Property
    Public Property kmCity As Integer
        Get
            Return pkmCity
        End Get
        Set(ByVal value As Integer)
            pkmCity = value
        End Set
    End Property
    Public Property kmStatus As Integer
        Get
            Return pkmStatus
        End Get
        Set(ByVal value As Integer)
            pkmStatus = value
        End Set
    End Property
    Public ReadOnly Property HasFinished As Boolean
        Get
            If kmStatus >= kmCity + kmHS + kmRG And kmStatus > 0 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property
    Public Property CarStartDate As Date
        Get
            Return pCarStartDate
        End Get
        Set(ByVal value As Date)
            pCarStartDate = value
            Dim e As New L3DObjectsLibrary.DateChangedEventArgs
            With e
                .TypeOfDateChanged = L3DObjectsLibrary.TypeOfDateChangedValue.DatePlan
                .StartEndDate = L3DObjectsLibrary.StartEndDateValue.StartDate
            End With
            RaiseEvent DateChanged(Me, e)
        End Set
    End Property
    Public Property CarEndDate As Date
        Get
            Return pCarEndDate
        End Get
        Set(ByVal value As Date)
            pCarEndDate = value
            Dim e As New L3DObjectsLibrary.DateChangedEventArgs
            With e
                .TypeOfDateChanged = L3DObjectsLibrary.TypeOfDateChangedValue.DatePlan
                .StartEndDate = L3DObjectsLibrary.StartEndDateValue.EndDate
            End With
            RaiseEvent DateChanged(Me, e)
        End Set
    End Property
    Public Property CarRealStartDate As Date
        Get
            Return pCarRealStartDate
        End Get
        Set(ByVal value As Date)
            pCarRealStartDate = value
            Dim e As New L3DObjectsLibrary.DateChangedEventArgs
            With e
                .TypeOfDateChanged = L3DObjectsLibrary.TypeOfDateChangedValue.DateReplan
                .StartEndDate = L3DObjectsLibrary.StartEndDateValue.StartDate
            End With
            RaiseEvent DateChanged(Me, e)
        End Set
    End Property
    Public Property CarRealEndDate As Date
        Get
            Return pCarRealEndDate
        End Get
        Set(ByVal value As Date)
            pCarRealEndDate = value
            Dim e As New L3DObjectsLibrary.DateChangedEventArgs
            With e
                .TypeOfDateChanged = L3DObjectsLibrary.TypeOfDateChangedValue.DateReplan
                .StartEndDate = L3DObjectsLibrary.StartEndDateValue.EndDate
            End With
            RaiseEvent DateChanged(Me, e)
        End Set
    End Property
    Public Property RGSpeed As Integer
        Get
            Return pRGSpeed
        End Get
        Set(ByVal value As Integer)
            pRGSpeed = value
        End Set
    End Property
    Public Property HSSpeed As Integer
        Get
            Return pHSSpeed
        End Get
        Set(ByVal value As Integer)
            pHSSpeed = value
        End Set
    End Property
    Public Property CitySpeed As Integer
        Get
            Return pCitySpeed
        End Get
        Set(ByVal value As Integer)
            pCitySpeed = value
        End Set
    End Property
    Public ReadOnly Property EndDateForecast(ByVal RefDate As Date) As Date
        Get
            Dim GiorniAlMese As Integer = 30
            Dim KmPerDay As Single = ((Me.CitySpeed + Me.HSSpeed + Me.RGSpeed) / GiorniAlMese) / 3 'divido per 3 mediando la velocità
            Dim RemKm As Integer = kmCity + kmHS + kmRG - kmStatus
            Dim DateBack As Date
            Dim DaysToEnd = RemKm / KmPerDay
            DateBack = DateAdd(DateInterval.Day, DaysToEnd, RefDate)
            Return DateBack
        End Get
    End Property
#End Region
#Region "METHODS"
    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return ObjManagement.GeneralFunctions.CloneObject(Me)
    End Function
    Public Function MileageForecast(ByVal CurrentStatusDate As Date, ByVal ForecastStatusDate As Date) As Integer
        Dim GiorniAlMese As Integer = 30
        Dim KmPerDay As Single = ((Me.CitySpeed + Me.HSSpeed + Me.RGSpeed) / GiorniAlMese) / 3 'divido per 3 mediando la velocità
        Dim DataRiferimento As Date
        If DateTime.Compare(CurrentStatusDate, Me.CarRealStartDate) < 0 Then
            DataRiferimento = Me.CarRealStartDate
        Else
            DataRiferimento = CurrentStatusDate
        End If
        Dim TimeToEnd As Integer = Math.Abs(DateDiff(DateInterval.Day, ForecastStatusDate, DataRiferimento))
        Dim KmForecast As Integer = TimeToEnd * KmPerDay + Me.kmStatus
        If KmForecast > Me.kmCity + Me.kmHS + Me.kmRG Then
            KmForecast = Me.kmCity + Me.kmHS + Me.kmRG
        End If
        If DateTime.Compare(CurrentStatusDate, ForecastStatusDate) > 0 Then
            KmForecast = Me.kmStatus
        End If
        Return KmForecast
    End Function
#End Region
#Region "EVENTS"

#End Region
End Class